package com.kgc.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kgc.api.buylimit.CouponApi;
import com.kgc.api.buylimit.TradeOrderApi;
import com.kgc.api.goods.OrderApi;
import com.kgc.api.user.UserReviverInfoApi;
import com.kgc.bean.DiscountTrick;
import com.kgc.bean.OrderInfo;

import com.kgc.bean.OrderItem;
import com.kgc.bean.UserReviverInfo;
import com.kgc.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Reference(timeout = 30000)
    private OrderApi orderApi;

    @Reference(timeout = 30000)
    private UserReviverInfoApi userReviverInfoApi;

    @Reference(timeout = 30000)
    private TradeOrderApi tradeOrderApi;

    @Reference(timeout = 30000)
    private CouponApi couponApi;



    @Override
    public OrderInfo queryOrderInfoByOrderId(String orderId) {

        return orderApi.queryOrderInfoByOrderId(orderId);

    }

    @Override
    public List<OrderInfo> queryAllOrdersByOrderState(Integer userId, Integer orderStateCode) {
        return orderApi.queryAllOrdersByOrderState(userId,orderStateCode);
    }

    @Override
    public List<OrderInfo> queryAllOrders(Integer userId) {
        return orderApi.queryAllOrders(userId);
    }

    @Override
    public UserReviverInfo queryGetUserReviverInfoById(Integer id) {
        // 获得用户收获地址
        return userReviverInfoApi.selectUserReviverInfoById(id);
    }

    @Async("asyncServiceExecutor")
    @Override
    public void querySaveOrderGoodsItem(List<OrderItem> orderItems) {
        tradeOrderApi.insertOrderItemByOrderItem(orderItems);
    }

    @Override
    public DiscountTrick selectDiscountTrickById(Integer id) {
        return couponApi.querySelectDiscountTrickById(id);
    }

    @Async("asyncServiceExecutor")
    @Override
    public void querySaveOrderGoodsInfo(OrderInfo orderInfo) {
        tradeOrderApi.insertOrderInfoByOrderInfo(orderInfo);

    }

    @Override
    public Boolean justyUserHaveDiscountTick(Integer userId, Integer discountId) {
        return couponApi.checkonly(userId,discountId);
    }

    @Override
    public Boolean justyUserUsedDiscountTick(Integer userId, Integer discountId) {
        return couponApi.justyUserUsedDiscount(userId,discountId);
    }

    @Override
    public Boolean queryUpdateOrderInfoByLoggistics(OrderInfo orderInfo) {

        return tradeOrderApi.updateOrderInfoByLoggistics(orderInfo);
    }


}
